package com.bite.book.controller;

import com.bite.book.enums.StatusEnum;
import com.bite.book.model.BookInfo;
import com.bite.book.service.BookService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RequestMapping("/book")
@RestController
public class BookController {

    @Autowired
    private BookService bookService;

    @RequestMapping("/getListByPage")
    public Result<PageResult<BookInfo>> getListByPage(RequestPage requestPage, HttpSession session){
        log.info("getListByPage 接收参数: {}", requestPage);
        //1. 参数校验/转换
        PageResult<BookInfo> result = bookService.getListByPage(requestPage);
        long end = System.currentTimeMillis();
        return Result.success(result);
    }

    @RequestMapping("/addBook")
    public Boolean addBook(BookInfo bookInfo){
        //1. 参数校验
        //2. 操作数据库
        //3. 返回结果
        if (!StringUtils.hasLength(bookInfo.getBookName())
                || !StringUtils.hasLength(bookInfo.getAuthor())
                || bookInfo.getCount()==null
                || bookInfo.getPrice() == null
                || !StringUtils.hasLength(bookInfo.getPublish())){
            log.warn("参数不正确");
            return false;
        }
        Integer result = bookService.addBook(bookInfo);
        if (result==0){
            return false;
        }
        return true;
    }

    @RequestMapping("/getBookInfo")
    public BookInfo getBookInfo(Integer bookId){
        log.info("getBookInfo,getId:{}",bookId);
        BookInfo bookInfo = bookService.getBookInfo(bookId);
        return bookInfo;
    }

    @RequestMapping("/updateBook")
    public Boolean updateBook(BookInfo bookInfo){

    }
}
